package com.see.domain.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.dbunit.DBTestCase;
import org.dbunit.DatabaseUnitException;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;

public class BaseDatabase extends DBTestCase{
	Connection conn = null;
	
	@Before
	public void setUp() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://172.16.5.70/stockexchangeengine","root", "root");
	}
	
	@After
	public void tearDown() throws SQLException {
		if(conn != null) {
			conn.close();
		}
	}
	
	 public BaseDatabase(String name)
	    {
	        super( name );
	        System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "com.mysql.jdbc.Driver" );
	        System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:mysql://172.16.5.70/stockexchangeengine" );
	        System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "root" );
	        System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "root" );
	    }
	@Override
	 protected DatabaseOperation getSetUpOperation() throws Exception {
		return DatabaseOperation.REFRESH;
	}

	@Override
	protected DatabaseOperation getTearDownOperation() throws Exception {
		return DatabaseOperation.NONE;
	}
	
	@Override
 protected void setUpDatabaseConfig(DatabaseConfig config) {
     config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE, new Integer(97));
 }

	@Override
	protected IDataSet getDataSet()throws SQLException,DatabaseUnitException{
		IDatabaseConnection connection = new DatabaseConnection(conn);
		IDataSet dataSet = connection.createDataSet();
		return dataSet;
	}
}
